package com.qmerak.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.qmerak.common.BSCalendar;
import com.qmerak.common.SysConstants;
import com.qmerak.dao.impl.ChangeUnionDao;
import com.qmerak.dao.impl.LogoffDao;
import com.qmerak.dao.impl.MemberDao;
import com.qmerak.dao.impl.OrgDao;
import com.qmerak.dao.impl.UnitDao;
import com.qmerak.entity.ChangeUnionEntity;
import com.qmerak.entity.LogoffEntity;
import com.qmerak.entity.MemberEntity;
import com.qmerak.entity.MemberEntity.Member_Status;
import com.qmerak.entity.OrgEntity;
import com.qmerak.entity.PostUnitEntity;
import com.qmerak.service.IMemberService;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-4-14
 * @project WebTest
 */
@Service
public class MemberServiceImpl implements IMemberService {
	private static Log logger = LogFactory.getLog(MemberServiceImpl.class);
	@Autowired
	private MemberDao memberDao;
	@Autowired
	private UnitDao unitDao;
	@Autowired
	private OrgDao orgDao;
	@Autowired
	private LogoffDao logoffDao;
	@Autowired
	private ChangeUnionDao changeUnionDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#add(com.entity.MemberEntity)
	 */
	@Override
	public MemberEntity add(MemberEntity entity) {
		if (null == entity.getIdNumber()) {
			logger.error("用户" + entity.getName() + "插入失败，没有身份证号！");
			return null;
		}
		String idCode = entity.getIdNumber();
		entity.setLoginName(idCode);
		entity.setMemberCode(idCode);
		initPWD(entity);
		return memberDao.add(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.qmerak.service.IMemberService#initPWD(com.qmerak.entity.MemberEntity)
	 */
	@Override
	public void initPWD(MemberEntity entity) {
		String idCode = entity.getIdNumber();
		int length = idCode.length();
		if (length > 6) {
			entity.setLoginPWD(idCode.substring(length - 6));
		} else {
			entity.setLoginPWD(idCode);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#delete(com.entity.MemberEntity)
	 */
	@Override
	public void delete(MemberEntity entity) {
		memberDao.remove(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#update(com.entity.MemberEntity)
	 */
	@Override
	public void update(MemberEntity entity) {
		MemberEntity oldEntity = memberDao.find(entity.getId());
		if (entity.getIdNumber() == null) {
			entity.setLoginName(oldEntity.getLoginName());
		} else if (entity.getIdNumber().equals(oldEntity.getIdNumber())) {
			entity.setLoginName(oldEntity.getLoginName());
		} else {
			entity.setLoginName(entity.getIdNumber());
		}
		entity.setLoginPWD(oldEntity.getLoginPWD());
		memberDao.update(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#list()
	 */
	@Override
	public List<MemberEntity> list() {
		return memberDao.list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#verify(com.entity.MemberEntity)
	 */
	@Override
	public boolean verify(MemberEntity userBean, ModelMap modelMap) {
		if (SysConstants.SYS_MANAGER_LOGINNAME.equals(userBean.getLoginName())) {
			if (userBean.getLoginPWD().equals(SysConstants.SYS_MANAGER_PWD)) {
				userBean.setName(SysConstants.SYS_MANAGER_NAME);
				userBean.setId(SysConstants.SYS_MANAGER_LOGINNAME);
				modelMap.addAttribute(SysConstants.SYS_LOGIN_USER, userBean);
				return true;
			} else {
				return false;
			}
		}
		MemberEntity userEntity = findByLoginName(userBean);
		if (userEntity == null) {
			return false;
		}
		if (MemberEntity.Member_Status.LOGOFFED.name().equals(
				userEntity.getStatus())) {
			return false;
		}
		if (userEntity.getLoginPWD().equals(userBean.getLoginPWD())) {
			modelMap.addAttribute(SysConstants.SYS_LOGIN_USER, userEntity);
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#listOfOrg(com.entity.OrgEntity)
	 */
	@Override
	public List<MemberEntity> listOfOrg(OrgEntity orgEntity) {
		return listOfOrg(orgEntity, null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.qmerak.service.IMemberService#listOfOrg(com.qmerak.entity.OrgEntity,
	 * java.lang.String)
	 */
	@Override
	public List<MemberEntity> listOfOrg(OrgEntity orgEntity, String condition) {
		List<MemberEntity> list = new ArrayList<MemberEntity>();
		List<PostUnitEntity> unitEntities = unitDao.unitsOfOrg(orgEntity);
		for (PostUnitEntity postUnitEntity : unitEntities) {
			list.addAll(memberDao.memberOfUnit(postUnitEntity, condition));
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#find(com.entity.MemberEntity)
	 */
	@Override
	public MemberEntity find(MemberEntity entity) {
		return memberDao.find(entity.getId());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#findMembers()
	 */
	@Override
	public List<MemberEntity> findMembers() {
		return memberDao.findMembers();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#loginingMemberOf(com.entity.MemberEntity)
	 */
	@Override
	public List<MemberEntity> loginingMemberOf(OrgEntity orgEntity) {
		List<MemberEntity> result = new ArrayList<MemberEntity>();
		for (PostUnitEntity unitEntity : unitDao.unitsOfOrg(orgEntity)) {
			result.addAll(memberDao.loginingMemberOfUnit(unitEntity));
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.service.IMemberService#logoffingMemberOf(com.entity.MemberEntity)
	 */
	@Override
	public List<MemberEntity> logoffingMemberOf(OrgEntity orgEntity) {
		List<MemberEntity> result = new ArrayList<MemberEntity>();
		for (PostUnitEntity unitEntity : unitDao.unitsOfOrg(orgEntity)) {
			result.addAll(memberDao.logoffingMemberOfUnit(unitEntity));
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.service.IMemberService#unionChangingMemberOf(com.entity.MemberEntity)
	 */
	@Override
	public List<MemberEntity> unionChangingMemberOf(OrgEntity orgEntity) {
		List<MemberEntity> result = new ArrayList<MemberEntity>();
		for (PostUnitEntity unitEntity : unitDao.unitsOfOrg(orgEntity)) {
			result.addAll(memberDao.unionChangingMemberOfUnit(unitEntity));
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#verifyID(com.entity.MemberEntity)
	 */
	@Override
	public boolean verifyIDNumber(MemberEntity memberEntity) {
		List<MemberEntity> list = memberDao.findByIDNumber(memberEntity);
		if (list.isEmpty()) {
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#updateStatus(com.entity.MemberEntity)
	 */
	@Override
	public void updateStatus(MemberEntity entity, MemberEntity loginMember) {
		MemberEntity oldEntity = memberDao.find(entity.getId());
		oldEntity.setStatus(entity.getStatus());
		switch (MemberEntity.Member_Status.valueOf(entity.getStatus())) {
		case LOGINED:
			if (MemberEntity.Member_Status.LOGINING.equals(entity.getStatus())) {
				oldEntity.setApproveDate(BSCalendar.now()
						.toString("yyyy-mm-dd"));
				oldEntity.setRoles("5");
			} else if (MemberEntity.Member_Status.UNIONCHANGING.equals(entity
					.getStatus())) {
				ChangeUnionEntity cuEntity = changeUnionDao
						.lastChangeUnion(entity.getId());
				oldEntity.setWorkUnit(cuEntity.getSourceUnion());
			}
			break;
		case LOGOFFED:
			LogoffEntity logoffEntity = logoffDao.lastLogoff(entity.getId());
			logoffEntity
					.setApproveDate(BSCalendar.now().toString("yyyy-mm-dd"));
			logoffEntity.setApprover(loginMember.getId());
			logoffDao.update(logoffEntity);
			break;
		case UNIONCHANGED:
			ChangeUnionEntity cuEntity = changeUnionDao.lastChangeUnion(entity
					.getId());
			cuEntity.setApproveDate(BSCalendar.now().toString("yyyy-mm-dd"));
			cuEntity.setApprover(loginMember.getId());
			oldEntity.setStatus(Member_Status.LOGINED.name());
			changeUnionDao.update(cuEntity);
			break;
		default:
			break;
		}
		memberDao.update(oldEntity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IMemberService#findByLoginName(com.entity.MemberEntity)
	 */
	@Override
	public MemberEntity findByLoginName(MemberEntity entity) {
		return memberDao.findByLoginName(entity.getLoginName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.qmerak.service.IMemberService#find(java.lang.String)
	 */
	@Override
	public MemberEntity find(String id) {
		if (SysConstants.SYS_MANAGER_LOGINNAME.equals(id)) {
			MemberEntity memberEntity = new MemberEntity();
			memberEntity.setLoginName(SysConstants.SYS_MANAGER_LOGINNAME);
			memberEntity.setId(SysConstants.SYS_MANAGER_LOGINNAME);
			memberEntity.setName(SysConstants.SYS_MANAGER_NAME);
			memberEntity.setLoginPWD(SysConstants.SYS_MANAGER_PWD);
			return memberEntity;
		}
		return memberDao.find(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.qmerak.service.IMemberService#updateAll(com.qmerak.entity.MemberEntity
	 * )
	 */
	@Override
	public void updateAll(MemberEntity entity) {
		memberDao.update(entity);
	}
}
